package Operation;

import book.Book;
import book.BookList;
import java.util.Scanner;

public class DelOperation implements IOperation{
    public void work(BookList bookList) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除书籍的书名：");
        String name = scanner.nextLine();
        int current = bookList.getUsedSize();
        int pos = -1;       //记录要删除书籍的下标
        for (int i = 0; i < current; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                pos = i;
                break;
            }
        }
        if(pos == -1) {
            System.out.println("要删除的书籍不存在");
        }else {
            for (int i = pos+1; i < current; i++) {
                bookList.getBooks()[i-1] = bookList.getBooks()[i];
            }
            bookList.getBooks()[current] = null;
            bookList.setUsedSize(current-1);
            System.out.println("删除成功");
        }
    }
}
